{% extends "base.html" %}

{% block title %}Users - Flask Database Demo{% endblock %}

{% block content %}
<h2>User Management</h2>

<div class="row">
    <div class="col-md-8">
        {% if users %}
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Member Since</th>
                            <th>Posts</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>{{ user.id }}</td>
                            <td>{{ user.username }}</td>
                            <td>{{ user.email }}</td>
                            <td>{{ user.member_since.strftime('%Y-%m-%d') }}</td>
                            <td>{{ user.posts.count() }}</td>
                            <td>
                                <a href="{{ url_for('user_posts', user_id=user.id) }}" class="btn btn-sm btn-outline-primary">View Posts</a>
                                <a href="{{ url_for('edit_user', user_id=user.id) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                                <a href="{{ url_for('delete_user', user_id=user.id) }}" class="btn btn-sm btn-outline-danger" 
                                   onclick="return confirm('Are you sure you want to delete this user?')">Delete</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <div class="alert alert-info">
                <p>No users found in the database.</p>
                <a href="{{ url_for('create_user') }}" class="btn btn-primary">Create First User</a>
            </div>
        {% endif %}
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5>User Statistics</h5>
            </div>
            <div class="card-body">
                <p><strong>Total Users:</strong> {{ users|length }}</p>
                <p><strong>Newest User:</strong> 
                    {% if users %}
                        {{ users[0].username }} ({{ users[0].member_since.strftime('%Y-%m-%d') }})
                    {% else %}
                        None
                    {% endif %}
                </p>
            </div>
        </div>
        
        <div class="card mt-3">
            <div class="card-header">
                <h5>About User Management</h5>
            </div>
            <div class="card-body">
                <p>This section demonstrates:</p>
                <ul>
                    <li>Querying all users from the database</li>
                    <li>Displaying user information in a table</li>
                    <li>Navigating to user-specific pages</li>
                    <li>Editing and deleting users</li>
                </ul>
            </div>
        </div>
    </div>
</div>

<div class="mt-3">
    <a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Home</a>
    <a href="{{ url_for('create_user') }}" class="btn btn-primary">Create New User</a>
</div>
{% endblock %}